/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cannes.dao.implement;

import cannes.bean.vip.Echange;
import cannes.connexion.ConnexionBDD;
import cannes.dao.DAO;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/**
 *
 * @author p1008977
 */
public class EchangeDAO extends DAO<Echange>{

    public EchangeDAO(Connection conn) {
        super(conn);
    }

    @Override
    public boolean create(Echange obj) {
        try
        {
        this.connect.createStatement(
                ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY).executeQuery(
                "INSERT INTO echange VALUES( " + obj.getVipId() + "," + obj.getId() +"," + obj.getDate() + "," + obj.getType() + "," + obj.getContenu());
        return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean update(Echange obj) {
        try
        {
        this.connect.createStatement(
                ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY).executeQuery(
                "UPDATE echange SET vipid= " + obj.getVipId() +",dateechange = " + obj.getDate() + ",typeechange" + obj.getType()+",contenu = " + obj.getContenu() + "WHERE echangeid =" + obj.getId());
        } catch (SQLException e) {
            e.printStackTrace();

        }
        return false;
    }

    @Override
    public boolean delete(Echange obj) {
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "DELETE FROM echange WHERE echangeid = " + obj.getId());
            result.rowDeleted();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public int next() {
        int i = 0;
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT max(echangeid) from echange");
            if (result.first()) i = result.getInt(1)+1;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }


    @Override
    public Echange find(int id) {
        Echange echange = new Echange();

        try {
            ResultSet res = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM echange WHERE echangeid = " + id);
            if (res.first()) {
                echange = new Echange(res.getInt("vipid"), res.getInt("echangeid"), res.getDate("dateechange"),
                        res.getString("typeechange"),res.getString("contenu"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return echange;

    }

    @Override
    public ArrayList<Echange> findAll() {
        ArrayList<Echange> list = new ArrayList<Echange>();
        Echange echange;

        try
        {
            ResultSet res = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM echange");
            while (res.next())
            {
                echange = new Echange(res.getInt("vipid"), res.getInt("echangeid"), res.getDate("dateechange"),
                        res.getString("typeechange"),res.getString("contenu"));
                list.add(echange);
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public ArrayList<Echange> findByVipID(int vipid) {
        ArrayList<Echange> list = new ArrayList<Echange>();
        Echange echange;

        try
        {
            ResultSet res = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM echange WHERE vipid =" + vipid);
            while (res.next())
            {
                echange = new Echange(res.getInt("vipid"), res.getInt("echangeid"), res.getDate("dateechange"),
                        res.getString("typeechange"),res.getString("contenu"));
                list.add(echange);
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    
    public ArrayList<Echange> findByVipId(int id) {
        ArrayList<Echange> list = new ArrayList<Echange>();
        Echange echange = new Echange();

        try
        {
            ResultSet res = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM echange WHERE vipid = "+id);
            while (res.next())
                echange = new Echange(res.getInt("vipid"), res.getInt("echangeid"), res.getDate("dateechange"),
                        res.getString("typeechange"),res.getString("contenu"));
                list.add(echange);
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

}
